classdef ziServerScopeModuleData < EquipmentControl.ZI.ziServer.ziServerData.ziServerData
    %ZISERVERSCOPEMODULEDATA �˴���ʾ�йش����ժҪ
    %   �˴���ʾ��ϸ˵��
    
    properties
        device
        domain
    end
    
    methods
        function obj = ziServerScopeModuleData(measured_data, nodeCell, device, domain)
            %ZISERVERSCOPEMODULE ��������ʵ��
            %   �˴���ʾ��ϸ˵��
            obj@EquipmentControl.ZI.ziServer.ziServerData.ziServerData('ziScopeData', measured_data, nodeCell);
            obj.device = device;
            obj.domain = domain;
        end
        
        %%
        function [xList, yList] = get_data(obj)
            xList = []; yList = [];
            if ~isempty(obj.measured_data)
                lastWave = obj.measured_data{end}.(obj.device).scopes.wave{end};
                switch obj.domain
                    case 'time'
                        yList = lastWave.wave;
                        xList = double(0:(lastWave.totalsamples-1)) * lastWave.dt;
                    case 'frequency'
                        dt = lastWave.dt; nSample = lastWave.totalsamples;
                        xList = linspace(0, 0.5/dt, nSample);
                        yList = lastWave.wave;
                end
            end
        end
        
        function plot_data(obj, fig_hdl, plot_win)
            if nargin == 2
                plot_win = 1;
            end
            if nargin == 1
                fig_hdl = figure;
                plot_win = 1;
            end
            
            legendString = cellfun(@(x) ['/' obj.device '/' x.toString], obj.data_node, 'UniformOutput', false);
            [xList, yList] = obj.get_data();
            
            if plot_win == 1
                clf(fig_hdl); ax = axes(fig_hdl);
                plot(ax, xList, yList, '.-');
                xlim(ax, minmax(xList));
                if strcmpi(obj.domain, 'frequency')
                    ax.YScale = 'log';
                    xlabel(ax, 'Frequency (Hz)');
                else
                    ax.YScale = 'linear';
                    xlabel(ax, 'Time (s)');
                end
                grid(ax, 'on');
                legend(ax, legendString);
            else
                clf(fig_hdl); 
                ax1 = subplot(2, 1, 1, 'Parent', fig_hdl);
                plot(ax1, xList, yList(:, 1), '.-');
                xlim(ax1, minmax(xList));
                if strcmpi(obj.domain, 'frequency')
                    ax1.YScale = 'log';
                    xlabel(ax1, 'Frequency (Hz)');
                else
                    ax1.YScale = 'linear';
                    xlabel(ax1, 'Time (s)');
                end
                grid(ax1, 'on');                
                legend(ax1, legendString{1});
                
                ax2 = subplot(2, 1, 2, 'Parent', fig_hdl);
                plot(ax2, xList, yList(:, 2), '.-');
                xlim(ax2, minmax(xList));
                if strcmpi(obj.domain, 'frequency')
                    ax2.YScale = 'log';
                    xlabel(ax2, 'Frequency (Hz)');
                else
                    ax1.YScale = 'linear';
                    xlabel(ax2, 'Time (s)');
                end
                grid(ax2, 'on');
                legend(ax2, legendString{2});
            end
                
        end
    end
end

